package dbindex;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import Ontology.*;

public class SliceStorage {

	public SliceStorage() {
	}
	
	private static void Insert ( java.sql.Connection cConn, Slicing isSlice, String isRequestText )
	{
		ByteArrayOutputStream baosSlice;
		ObjectOutputStream oosSlice;

		Statement   s = null;
        ResultSet   rs = null;
        PreparedStatement pstmt = null;
        
        try {
			baosSlice = new ByteArrayOutputStream();
			oosSlice = new ObjectOutputStream(baosSlice);
			
			oosSlice.writeObject(isSlice);
			oosSlice.flush();
	        try
	        {
	            pstmt = cConn.prepareStatement("Insert into slicestorage (RequestText, Slice) Values ('" + isRequestText + "', ?)");
	            pstmt.setBytes( 1, baosSlice.toByteArray());
	            pstmt.executeUpdate();

	        } catch(SQLException e) {
	        	System.out.println(e);
	        	System.out.println(pstmt.toString());
	        } finally {
	            if (rs != null) {   try { rs.close(); } catch (SQLException sqlEx) { }  rs = null; }
	            if (s != null)  {   try { s.close();  } catch (SQLException sqlEx) { }  s = null;  }
	        }
            
	        baosSlice.close();
        } catch ( Exception e )
		{
		 	System.out.println(e); 
		}
    }
	
	private static Slicing GetByRequest( java.sql.Connection cConn, String isRequestText )
	{
		ByteArrayInputStream baisSlice;
		ObjectInputStream oisSlice;

        ResultSet   rs = null;
        PreparedStatement pstmt = null;
        
        Slicing sTemp = null;

        try {
            
            pstmt = cConn.prepareStatement("Select Slice from slicestorage where RequestText = '" + isRequestText + "'");
            pstmt.executeQuery();
            rs = pstmt.getResultSet ();
            if (rs.next ())
            {
        		try 
        		{
        			baisSlice = new ByteArrayInputStream(rs.getBytes(1));
        			oisSlice = new ObjectInputStream(baisSlice);
        			
        			sTemp = (Slicing) oisSlice.readObject();
        			
        			baisSlice.close();

        		} catch ( Exception e )
        		{
        		 	System.out.println(e); 
        		}
            }
        } catch(SQLException e) {
        	System.out.println(e);
        } finally {
            if (rs != null) {   try { rs.close(); } catch (SQLException sqlEx) { }  rs = null; }
            if (pstmt != null)  {   try { pstmt.close();  } catch (SQLException sqlEx) { }  pstmt = null;  }
        }
        return sTemp;
    }

	private static Slicing GetByID( java.sql.Connection cConn, Integer i_id )
	{
		ByteArrayInputStream baisSlice;
		ObjectInputStream oisSlice;

        ResultSet   rs = null;
        PreparedStatement pstmt = null;
        
        Slicing sTemp = null;

        try {
            
            pstmt = cConn.prepareStatement("Select Slice from slicestorage where id = " + i_id);
            pstmt.executeQuery();
            rs = pstmt.getResultSet ();
            if (rs.next ())
            {
        		try 
        		{
        			baisSlice = new ByteArrayInputStream(rs.getBytes(1));
        			oisSlice = new ObjectInputStream(baisSlice);
        			
        			sTemp = (Slicing) oisSlice.readObject();
        			
        			baisSlice.close();

        		} catch ( Exception e )
        		{
        		 	System.out.println(e); 
        		}
            }
        } catch(SQLException e) {
        	System.out.println(e);
        } finally {
            if (rs != null) {   try { rs.close(); } catch (SQLException sqlEx) { }  rs = null; }
            if (pstmt != null)  {   try { pstmt.close();  } catch (SQLException sqlEx) { }  pstmt = null;  }
        }
        return sTemp;
    }

}
